CMAKE_MINIMUM_REQUIRED(VERSION 2.8.9)

SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules)

INCLUDE(CheckCXXCompilerFlag)
INCLUDE(AndroidBuildEnvironment)

IF(ANDROID_BUILD_ENV)
    IF(ANDROID_CPU_ARCH)
        SET(SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS ${SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS} CPU_ARCH ${ANDROID_CPU_ARCH})
    ENDIF()
    IF(ANDROID_PLATFORM)
        SET(SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS ${SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS} PLATFORM ${ANDROID_PLATFORM})
    ENDIF()
    IF(ANDROID_NDK_PATH)
        SET(SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS ${SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS} ANDROID_NDK_PATH ${ANDROID_NDK_PATH})
    ENDIF()
    SETUP_ANDROID_BUILD_ENVIRONMENT(${SETUP_ANDROID_BUILD_ENVIRONMENT_ARGS})
ENDIF(ANDROID_BUILD_ENV)

ADD_DEFINITIONS(-D__GXX_EXPERIMENTAL_CXX0X__)

PROJECT(Carina)

ENABLE_TESTING()

IF(MINGW AND NOT DISABLE_MINGW_SETUP AND WIN32)
	INCLUDE(WindowsMinGWBuildEnvironment)
	MINGW_BUILD_ENVIRONMENT()
ENDIF()

IF(UNIX)
    ADD_DEFINITIONS(-DLINUX)
ENDIF()

IF(MSVC)
    SET(SIMD_FLAGS "/arch:SSE")
    SET(HAS_SSE on)
ELSE(MSVC)
    CHECK_CXX_COMPILER_FLAG(-msse HAS_SSE)
    IF(HAS_SSE)
        SET(SIMD_FLAGS "-msse")
    ELSE(HAS_SSE)
        CHECK_CXX_COMPILER_FLAG(-mfpu=neon HAS_ARM_NEON)
        IF(HAS_ARM_NEON)
            SET(SIMD_FLAGS -mfpu=neon)
        ELSE(HAS_ARM_NEON)
            MESSAGE(WARNING "No supported SIMD instruction set has been found.")
        ENDIF(HAS_ARM_NEON)
    ENDIF(HAS_SSE)
ENDIF(MSVC)

SET(CMAKE_VERBOSE_MAKEFILE on)

SET(Carina_VERSION_MAJOR 0)
SET(Carina_VERSION_MINOR 1)
SET(Carina_VERSION 0.1)

SET(CARINA_COMMON_INCLUDE_DIRS
    ${CMAKE_SOURCE_DIR}/carina_common/include/
    ${CMAKE_BINARY_DIR}/carina_common/include/
   )

SET(CARINA_ENGINE_INCLUDE_DIRS
    ${CMAKE_BINARY_DIR}/carina_engine/include/
    ${CMAKE_SOURCE_DIR}/carina_engine/include/
    ${CARINA_COMMON_INCLUDE_DIRS}
    ${CMAKE_SOURCE_DIR}/bundle
   )

IF(CMAKE_COMPILER_IS_GNUCXX)
    EXEC_PROGRAM(${CMAKE_CXX_COMPILER}
                 ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
                 OUTPUT_VARIABLE GCC_COMPILER_VERSION)
    IF(GCC_COMPILER_VERSION VERSION_LESS "4.3")
        MESSAGE(FATAL_ERROR "Unsupported compiler version. Please update to GCC >= 4.3")
    ELSE()
        ADD_DEFINITIONS("-pedantic -Wall -Wno-long-long -std=c++0x")
        IF(GCC_COMPILER_VERSION VERSION_LESS "4.7")
            SET(HAS_NO_CXX11_EXPLICIT_OVERRIDES on)
        ENDIF()
    ENDIF()
ELSEIF(MSVC)
    IF(MSVC_VERSION VERSION_GREATER 1600)
		SET(CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012" CACHE STRING "Platform Toolset" FORCE) 
    ELSE()
        MESSAGE(FATAL_ERROR "Unsupported compiler version. You need at least VS 2010.")
    ENDIF()
ELSEIF(IS_CLANG)
    ADD_DEFINITIONS("-std=c++11")
    FIND_LIBRARY(HAS_LIBCXX NAMES c++)
    IF(HAS_LIBCXX)
        ADD_DEFINITIONS("-stdlib=libc++")
    ENDIF()
ELSE()
    MESSAGE(FATAL_ERROR "Unsupported compiler.")
ENDIF()

FIND_PACKAGE(Doxygen)
FIND_PACKAGE(LATEX)
FIND_PACKAGE(PNG)
IF(PNG_FOUND)
    SET(HAS_LIBPNG on)
ENDIF()

IF(POLICY CMP0020)
    CMAKE_POLICY(SET CMP0020 OLD)
ENDIF()
FIND_PACKAGE(Qt5Core)
IF(Qt5Core_FOUND)
    SET(HAS_QT5 on)
	SET(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage")
ENDIF()

IF(DOXYGEN_FOUND AND LATEX_COMPILER)
    SET(CARINA_DOXYFILE ${CMAKE_BINARY_DIR}/Doxyfile)

    CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/Doxyfile.in
                   ${CARINA_DOXYFILE}
                   @ONLY)

    ADD_CUSTOM_TARGET(carina_docs
                      ${DOXYGEN_EXECUTABLE} ${CARINA_DOXYFILE} 
                      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                      VERBATIM)
ENDIF(DOXYGEN_FOUND AND LATEX_COMPILER)

SET(Carina_TESTS_DIR "${CMAKE_BINARY_DIR}/tests")

IF(HAS_QT5 AND NOT WIN32)
    SET(CARINA_DEBUG_LIBRARIES ${QT_LIBRARIES})
ENDIF()

ADD_SUBDIRECTORY(carina_engine)
ADD_SUBDIRECTORY(carina_common)
ADD_SUBDIRECTORY(ces_translators)
ADD_SUBDIRECTORY(carina_tools)
ADD_SUBDIRECTORY(tests)
